package com.micrometa.km.ui.components.processbar;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import com.micrometa.km.ui.components.tree.FileTreeNode;
import com.micrometa.km.ui.components.tree.FileTreeNodeUtil;
import com.micrometa.km.util.FileUtil;
import com.micrometa.km.util.PropertyConstants;
import com.micrometa.km.util.PropertyUtil;

public class ProcessBar extends Thread {
	private static final long serialVersionUID = 1L;
	private JProgressBar progressBar;
	private List<File> fileList;
	private File tarDir;
	private static int DELAY = 300;
	private JTree tree;
	private FileTreeNode parnode;

	public ProcessBar(JProgressBar bar, final List<File> fileList, File tarDir) {
		progressBar = bar;
		this.fileList = fileList;
		this.tarDir = tarDir;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				progressBar.setOrientation(0);
				progressBar.setMinimum(0);
				progressBar.setMaximum(fileList.size());
				progressBar.setVisible(true);
			}
		});
	}
	
	public void setTree(JTree tree,FileTreeNode parnode){
		this.tree=tree;
		this.parnode=parnode;
	}

	public void run() {
		String action = PropertyUtil.action();
//		System.out.println(fileList.size());
		for (File f : fileList) {
			// copy the file to repository
			final File n = new File(tarDir.getAbsolutePath() + "/" + f.getName());
			if (PropertyConstants.ACTION_COPY.equals(action))
				try {
					FileUtil.copyAll(f, n);// copy f to n
				} catch (IOException e) {
					JOptionPane.showMessageDialog(null, "文件‘" + f.getName()
							+ "’拒绝访问");
				}
			else if (PropertyConstants.ACTION_CUT.equals(action))
				f.renameTo(n);// cut f to n
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					int i = progressBar.getValue();
					progressBar.setValue(i + 1);
					progressBar.setString((i+1)+"."+n.getName());
				}
			});
			try {
				Thread.sleep(DELAY);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		refreshTree();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				progressBar.setVisible(false);
				progressBar.setValue(0);
			}
		});
	}
	
	private void refreshTree(){
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				FileTreeNode tarn = FileTreeNodeUtil.getFileTreeNodes(parnode
						.getFile());
				DefaultTreeModel model=(DefaultTreeModel) tree.getModel();
				model.insertNodeInto(tarn,
						(MutableTreeNode) parnode.getParent(), parnode.getParent()
								.getIndex(parnode));
				model.removeNodeFromParent(parnode);
				tree.expandPath(new TreePath(((DefaultTreeModel) tree.getModel())
						.getPathToRoot(tarn)));
			}
		});
	}

	public static void main(String[] args) {
		final List fl = new ArrayList();
		fl.add(new File("c://test/a.txt"));
		fl.add(new File("c://test/b.txt"));
		fl.add(new File("c://test/c.txt"));
		fl.add(new File("c://test/d.txt"));
		fl.add(new File("c://test/ipmsg.jar"));
		fl.add(new File("c://test/wmkm.jar"));
		final JProgressBar aJProgressBar = new JProgressBar(0, fl.size());
		aJProgressBar.setStringPainted(true);
		final JButton aJButton = new JButton("Start");
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				aJButton.setEnabled(false);
				Thread stepper = new ProcessBar(aJProgressBar, fl, new File(
						"c://test/copy"));
				stepper.start();
			}
		};
		aJButton.addActionListener(actionListener);
		JFrame theFrame = new JFrame("Progress Bars");
		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = theFrame.getContentPane();
		contentPane.add(aJProgressBar, BorderLayout.CENTER);
		contentPane.add(aJButton, BorderLayout.SOUTH);
		theFrame.setSize(300, 100);
		theFrame.setVisible(true);
	}
}
